package pay.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;
import pay.adapter.EncashAdapter;
import pay.bean.EncashHistoryBean;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/4/27.
 * 提现记录
 */
@ContentView(R.layout.encash_history)
public class EnCashHistory extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.common_title)
    private TextView common_title;
    @ViewInject(R.id.encash_history_load_layout)
    private MyPtrRefresh_LoadView encash_history_load_layout;
    @ViewInject(R.id.encash_history_lv)
    private ListView encash_history_lv;
    @ViewInject(R.id.encash_history_nohistory)
    private LinearLayout encash_history_nohistory;
    private EncashAdapter encashAdapter;
    private ArrayList<EncashHistoryBean.RecordBean> arrayList;
    private View headerView;
    private TextView total;//总金额


    private int page;
    private final int INIT = 0;
    private final int LOAD = 1;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    EncashHistoryBean encashHistoryBean = gson.fromJson((String) msg.obj, EncashHistoryBean.class);
                    if (encashHistoryBean != null) {
                        if ("0".equals(encashHistoryBean.getErr_code())) {
                            //有数据
                            encash_history_load_layout.setVisibility(View.VISIBLE);
                            encash_history_nohistory.setVisibility(View.GONE);
                            if (page == 1) {
                                total.setText("¥" + encashHistoryBean.getSum());
                                List<EncashHistoryBean.RecordBean> record = encashHistoryBean.getRecord();
                                if (record != null) {
                                    if (record.size() > 0) {
                                        arrayList.clear();
                                        for (int i = 0; i < record.size(); i++) {
                                            arrayList.add(record.get(i));
                                        }
                                    } else {
                                        if (msg.arg1 == LOAD) {
                                            page--;
                                        }
                                    }
                                    encashAdapter.notifyDataSetChanged();
                                }
                            } else {
                                List<EncashHistoryBean.RecordBean> record = encashHistoryBean.getRecord();
                                if (record != null) {
                                    if (record.size() > 0) {
                                        for (int i = 0; i < record.size(); i++) {
                                            arrayList.add(record.get(i));
                                        }
                                    } else {
                                        if (msg.arg1 == LOAD) {
                                            page--;
                                        }
                                    }
                                    encashAdapter.notifyDataSetChanged();
                                }
                            }
                        } else {
                            encash_history_load_layout.setVisibility(View.GONE);
                            encash_history_nohistory.setVisibility(View.VISIBLE);
                        }
                    }
                    break;
                case Constant.ERROR_CODE_FINISH:
                    encash_history_load_layout.refreshComplete();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        getDataFromInterNet(page = 1, INIT);
    }

    private void initView() {
        common_back.setOnClickListener(this);
        common_title.setText("提现记录");
        encash_history_load_layout.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, LOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, INIT);
            }
        });
        headerView = LayoutInflater.from(this).inflate(R.layout.encash_header, null);
        total = (TextView) headerView.findViewById(R.id.encash_history_total);
        arrayList = new ArrayList<>();
        encashAdapter = new EncashAdapter(this, arrayList);
        encash_history_lv.addHeaderView(headerView);
        encash_history_lv.setAdapter(encashAdapter);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            default:
                break;
        }
    }

    private void getDataFromInterNet(int page, int flag) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("page", String.valueOf(page));
        new InterNetController(this, Constant.RECORD, handler, hashMap, flag);
    }
}
